package com.ctn.information.data.retriever.impl.sample;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ctn.information.data.model.NewsComment;
import com.ctn.information.data.retriever.Interface.NewsCommentRetriever;

/**
 * Sample Implementation for test only
 * @author Zale
 *
 */
public class NewsCommentRetrieverSampleImpl implements NewsCommentRetriever {
	private List<NewsComment> comments;
	
	public NewsCommentRetrieverSampleImpl() {
		super();
		initDefaults();
	}

	private void initDefaults() {
		if(comments == null){
			comments = new ArrayList<NewsComment>();
		}
		NewsComment comment1 = createComment(1L,"不错，很好1",new Date(),null);
		comments.add(comment1);
		NewsComment comment2 = createComment(2L,"不错，很好2",new Date(),comment1);
		comments.add(comment2);
		comments.add(createComment(3L,"不错，很好3",new Date(),comment1));
		comments.add(createComment(4L,"不错，很好4",new Date(),comment2));
		
	}

	private NewsComment createComment(Long id, String content, Date date,
			NewsComment parent) {
		NewsComment comment = new NewsComment();
		comment.setCommnet(content);
		comment.setId(id);
		comment.setCreateDate(date);
		if(parent !=null){
			comment.setParent(parent);
		}
		return comment;
		
	}

	@Override
	public NewsComment getById(Long id) {
		for(NewsComment comment:comments){
			if(comment.getId()==id){
				return comment;
			}
		}
		return null;
	}

	@Override
	public List<NewsComment> getAll(boolean asc) {
		return comments;
	}
	/**
	 * @param count >0 大于些ID的count 条记录。
	 */
	@Override
	public List<NewsComment> getDataFromId(Long id, int count, boolean asc) {
		List<NewsComment> results = new ArrayList<NewsComment>();
		for(NewsComment comment:comments){
			if(count>0){
				if(comment.getId()>id){
					results.add(comment);
					if(results.size()>count){
						break;
					}
				}
			}else{
				if(comment.getId()<id){
					results.add(comment);
					if(results.size()>-count){
						break;
					}
				}
			}
		}
		return results;
	}

	@Override
	public List<NewsComment> getDataFromDate(Date date, int count, boolean asc) {
		List<NewsComment> results = new ArrayList<NewsComment>();
		for(NewsComment comment:comments){
			if(count>0){
				if(comment.getCreateDate().after(date)){
					if(results.size()>count){
						break;
					}
						results.add(comment);
				}
			}else{
				if(comment.getCreateDate().before(date)){
					if(results.size()>-count){
						break;
					}
					results.add(comment);
			}
			}
		}
		return results;
	}

}
